#include "main.h"

#include <shlobj_core.h>
#include "Hook_Scaleform.h"
#include "my_classes/Forms.h"
#include "my_classes/GameInput.h"
#include "my_classes/MessageMenuManager.h"
#include "my_classes/Shared.h"
#include "Relocation/RVA.h"
#include "sfse_common/Log.h"
#include "sfse/PluginAPI.h"
#include "sfse_common/sfse_version.h"
#include "my_classes/UI.h"

SFSEMessagingInterface* g_messaging = nullptr;

struct Unk
{
    void* unk00;
    void* unk08;
};
typedef Unk* (*_Unkf)(TESForm* form, Unk& unk);
extern RelocAddr<_Unkf> Unkf(0x14DB1A4);

typedef void (*_TESFullName__SetFullName)(TESFullName* name, BSFixedString* newname);
extern RelocAddr<_TESFullName__SetFullName> TESFullName__SetFullName(0x14DB1A4);

BSEventNotifyControl	TESLoadGameHandler::ProcessEvent(const TESLoadGameEvent& a_event, BSTEventSource<TESLoadGameEvent>* a_source)
{
    _DMESSAGE("------------------------------------TESLoadGameEvent recieved-------------------------------");
   // auto uuu = new Unk();
  //  Unkf(*g_player, *uuu);
   // (*g_player)->GetFullName()
   // DumpClass(uuu, 5);
    //DumpClass(MenuControls::GetSingleton(), 30);
    //DumpClass(MenuControls::GetSingleton()->inputEvents.pData, 10);
    //DumpClass(*g_player, 200);
    /*
    auto armo = LookupByID(0x00225FC9);
    DumpClass( armo, 0x300/8);
    auto off118 = GetOffset<TESFullName>(armo, 0x118);
    _MESSAGE("%s", off118.strFullName.c_str());
    _MESSAGE("%s", armo->GetTextForParsedSubTag());
    auto aaa = BSFixedString("zhopa");
    //TESFullName__SetFullName(&off118, &aaa);
    //armo->AddChange(4);
    off118.strFullName = aaa;
    _MESSAGE("%s", off118.strFullName.c_str());
    _MESSAGE("%s", armo->GetTextForParsedSubTag());
    */
    auto player = *g_player;
    auto offC0 = GetOffset<TESObjectREFR*>(player, 0xC0);
    auto off1F8 = GetOffset<TESFullName>(offC0, 0x1F8);
    BSFixedString aaa = BSFixedString("zhopa");
    TESFullName__SetFullName(&off1F8, &aaa);
    offC0->AddChange(0x20);
    
    return BSEventNotifyControl::kContinue;
}

EventResult	MenuOpenCloseEventHandler::ProcessEvent(const MenuOpenCloseEvent& a_event, BSTEventSource<MenuOpenCloseEvent>* a_source)
{
    _DMESSAGE("-------------MenuOpenCloseEvent recieved-------- %s %i-----------", a_event.MenuName.c_str(), a_event.bOpening);
    Debug_Notification("-MenuOpenCloseEvent recieved-");
    return BSEventNotifyControl::kContinue;
}

void MessageCallback(SFSEMessagingInterface::Message* msg)
{
    switch (msg->type)
    {
    case SFSEMessagingInterface::kMessage_PostPostLoad:
        _MESSAGE("postpostload");
        GetEventSource<TESLoadGameEvent>()->RegisterSink(new TESLoadGameHandler());
        //(&(*g_UI)->menuOpenCloseEventSource)->RegisterSink(new MenuOpenCloseEventHandler());

        break;
    }
}

extern "C"
{
    __declspec(dllexport) SFSEPluginVersionData SFSEPlugin_Version =
    {
        SFSEPluginVersionData::kVersion,	
        1,
        "my_test",
        "Neanka",
        0,	// not address independent
        0,	// not structure independent
        { CURRENT_RELEASE_RUNTIME, 0},	// compatible with 1.7.23 and that's it
        0,	// works with any version of the script extender. you probably do not need to put anything here
        0, 0,	// set these reserved fields to 0
    };
    
    __declspec(dllexport) bool SFSEPlugin_Load(const SFSEInterface *sfse)
    {
        DebugLog::openRelative(CSIDL_MYDOCUMENTS, "\\My Games\\Starfield\\SFSE\\Logs\\my_test.txt");
        _MESSAGE("load");

        g_messaging = static_cast<SFSEMessagingInterface*>(sfse->QueryInterface(kInterface_Messaging));
        if (!g_messaging)
        {
            _FATALERROR("couldn't get messaging interface");
            return false;
        }
        g_messaging->RegisterListener(sfse->GetPluginHandle(), "SFSE", MessageCallback);

        // sig scan 
        plugin_info.plugin_name="my_test";
        plugin_info.runtime_version = CURRENT_RELEASE_RUNTIME;
        sig_scan_timer tmr; tmr.start();
        //RELOC_GLOBAL_VAL(g_UI, "48 89 05 ? ? ? ? 48 8B 0F 48 85 C9", 0, 3, 7)
        RELOC_GLOBAL_VAL(g_player, "83 FE 02 0F 85 ? ? ? ? EB ?", 0x17, 3, 7)
        _MESSAGE("Sigscan elapsed: %llu ms.", tmr.stop());
        return true;
    }

};